package View;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Model.Comparison;

public class StringGui implements ActionListener{
	public JFrame frame;
	public JPanel panel;
	public JTextField field;
	public JButton compareBtn;
	public processing process = new processing();
	public StringResult res = new StringResult();
	
	public StringGui(){
		frame = new JFrame();
		panel = new JPanel();
		field = new JTextField(50);
		compareBtn = new JButton("Compare");
		frame.setBounds(50,50,600,100);
		panel.add(field);
		panel.add(compareBtn);
		frame.add(panel);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		compareBtn.addActionListener(this);
	}
	public void setVisible(){
		frame.setVisible(true);
	}
	public void closeVisible(){
		frame.setVisible(false);
	}
	public void actionPerformed(ActionEvent arg0) {
		closeVisible();
		Comparison cp = new Comparison();
		try {
			String [] data = cp.filesRead(Comparison.path);
			System.out.println("first File"+data[0]);
			File dir = new File(Comparison.path);
			File [] files = dir.listFiles();
			String data1 = this.field.getText();
			System.out.println("Data in field = "+data1);
			res.panels = new JPanel[files.length];
			res.frame.setLayout(new GridLayout(files.length,1));
			System.out.println(files[0].getName());
			for(int i = 0;i<files.length;i++){
				System.out.println(data[i]);
				if(data[i].toLowerCase().contains(data1.toLowerCase())){
					res.panels[i] = new JPanel();
					res.panels[i].add(new JLabel(files[i].getName()));
					res.frame.add(res.panels[i]);
				}
			}
			res.setVisible();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
